<?php

/**
 * CelebrioWeather default
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    CelebrioWeather
 */

namespace ApplicationsModule\WeatherModule;

use WebtopModule\BasePresenter;

class DefaultPresenter extends BasePresenter {

    private $place = "786869"; //Brno

    public function startup() {
        parent::startup();
        $this->getTemplate()->title = _("Weather");
        $this->getTemplate()->appName = "weather";
        header("Content-type:text/html; charset=utf-8"); // TODO: poresit
    }

    public function renderDefault() {
        
    }

    public function renderWeather($place) {
        $this->place = $place;
        $this->invalidateControl("celebrioweatherappSnippet");
        //$this->template->currentTime = date("H:i"); // TODO: nationalize the time
        $this->redrawWeather();
    }

    private function redrawWeather() {
        $this->template->currentTime = date("H:i"); // TODO: nationalize the time
        try {
            $weather = new WeatherForecast($this->place);
        } catch (\InvalidStateException $e) {
            // TODO: log the error
            return;
        }
        $this->template->city = $weather->getCity();
        $this->template->region = trim($weather->getRegion());
        $this->template->country = trim($weather->getCountry());
        $this->template->currentTemperature = $weather->getCurrentTemperature();
        $currentInfo = $weather->getCurrentTextAndImage();
        $this->template->currentText = $currentInfo["text"];
        $this->template->currentImage = $currentInfo["image"];
        $todayTemperatures = $weather->getTodayTemperatures();
        $this->template->todayLow = $todayTemperatures["low"];
        $this->template->todayHigh = $todayTemperatures["high"];
        $todayInfo = $weather->getTodayTextAndImage();
        $this->template->todayText = $todayInfo["text"];
        $this->template->todayImage = $todayInfo["image"];
        $tomorrowTemperatures = $weather->getTomorrowTemperatures();
        $this->template->tomorrowLow = $tomorrowTemperatures["low"];
        $this->template->tomorrowHigh = $tomorrowTemperatures["high"];
        $tomorrowInfo = $weather->getTomorrowTextAndImage();
        $this->template->tomorrowText = $tomorrowInfo["text"];
        $this->template->tomorrowImage = $tomorrowInfo["image"];
        unset($weather);
    }
}